<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${r'${pageContext.request.contextPath}'}"/>
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <%@ include file="/common/${projectName}/include/common-base.jsp"%>
  <title>${template.titleName}</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>

<!-- Part 一、 以下是body主体内容的模块区域 -->
<body class="hold-transition ${r'${sysColor}'} sidebar-mini">
<div class="wrapper">

  <!-- top start -->
  <%@include file="/common/${projectName}/top.jsp" %>
  <!-- top end -->
  <!-- left start -->
  <%@include file="/common/${projectName}/left.jsp" %>
  <!-- left end-->

  <div class="content-wrapper">
    <!-- Main start -->
	<section class="content">
	  			<div id="div_hidden_content" class="hidden" style="display:none;">
	  			    <input name="templateType" type="hidden" value="edit"/>
                    <div id="hidden_common_final">
                        <!-- temp="hidden_common_final" start -->
                        <!-- temp="hidden_common_final" end -->

                        <form id="viewForm" action="javascript:;">
                        <!-- temp="viewForm_hidden" start -->
                            <div id="viewForm_hidden"></div>
                        <!-- temp="viewForm_hidden" end -->
                        </form>
                     </div>
	  			</div>
      <div class="row">
        <div class="col-lg-12">
        
          <div class="box box-info">
	          <div class="box-header with-border">
                 <h3 class="box-title"><i class="fa fa-edit"></i> ${template.titleName}</h3>
              </div>
            <div class="box-body">
            	<form class="form-horizontal editFormBody" id="editForm1" action="javascript:void(0)">
					<input type="hidden" class="formToken" id="editForm1_formToken" name="formToken" />
					<!-- temp="editForm1_hidden" start -->
					<div id="editForm1_hidden"></div>
					<!-- temp="editForm1_hidden" end -->
					
					<#list detailList as detail>
					<#assign tabNo = detail.tabNo>
					<div class="panel">
						<div class="panel-heading">
							<h4><b>| </b> ${detail.tabName!''}</h4>
						</div>
						<div class="panel-body">
						
							<!-- temp="${detail.formType!'editForm'}1_content_${tabNo}" start -->
							<div id="${detail.formType!'editForm'}1_content_${tabNo}" data-temp-code="true"
								data-temp-type="${detail.formType!'editForm'}" data-temp-form-id="${detail.formType!'editForm'}_content_${tabNo}">
							</div>
							<!-- temp="${detail.formType!'editForm'}1_content_${tabNo}" end -->
							
						</div>
					</div>
					</#list>
            	</form>
            </div>
            
             <div class="box-footer text-center">
			       <button type="button" class="btn btn-success" onclick="submitForm1('editForm1','');" data-temp-event="editForm1_hidden"><i class="fa fa-save"></i> 提交保存 </button> 
			       <button class="btn btn-warning" type="button" onclick="window.history.back();"> 返回</button>
			 </div>
	     </div>
           
       
        </div>
      </div>
  

<!-- Part 二、 以下是js模块区域 -->
<script>
var clickMenu = "${template.menuId}";
/**
 * 初始化数据字典，id传入#id 返回值Map用法：dictListMap["userType"][rows[i].userType]
 */
var dictListMap = DictUtil.initDictAndDymic();
 
$(function () {
	mytools.cleanData(".editFormBody");//清空表单元素上次填写的内容；
	setValidate(".editFormBody");//定义add表单验证属性
	mytools.cleanValidate(".editFormBody");//清空上次校验规则提示
	
	var mainObjValue = $("#hidden_common_final").find("input[name='mainObjName']").val();//对象
	var primaryKeyValue = $("#hidden_common_final").find("input[name='mainObjPrimaryKey']").val();//主键
	var primaryParamName = mainObjValue+".w_"+primaryKeyValue;//条件
	var primaryKeyValue = $("#viewForm").find("input[name='"+primaryParamName+"']").val();//参数值
	if(!strUtil.isNull(primaryKeyValue)){
		getInfoById();
	}

});
function getInfoById(){
	$.get(common_info_url, $("#viewForm").serialize(),function(myData){
		console.log(myData);
		//反显加载数据
		if(myData){
			for(var objNameKey in myData){
				FormUtil.loadView(objNameKey, myData[objNameKey]);
			}
		}
   	},"json");
}
//注入日历框样式
var dateObject = $('.datepicker').datepicker( {
	language : 'zh-CN',
	//inline: true,
	todayBtn : "linked",
	format : "yyyy-mm-dd",
	autoclose: true
}).on('hide',function(e) {
	//bootstrap datepicker与bootstrapValidator同时使用时选择日期后无法正常触发校验的解决方法
	//当日期选择框关闭时，执行刷新校验。
	var fieldName = $(this).attr("name");//获取当前元素name
	var formId = $(this).parents("form").eq(0).attr("id");//获取当前元素所在父层级中的就近的formId
    $("#"+formId).data('bootstrapValidator').updateStatus(fieldName,'NOT_VALIDATED',null).validateField(fieldName);
});

//提交表单数据
function submitForm1(formId,tabId){
	//校验规则-表单提交时触发
	var validate = $('#'+formId).data('bootstrapValidator').validate();
	if(!validate.isValid()){ //如果校验不通过就返回
	    return;  
	}
	var index_submit = layer.confirm('您确定要保存信息吗？', {btn: ['Yes','No'],closeBtn: 0,offset: '220px'}
	, function(){
		var result = submitForm(formId,tabId);
		if(result){
      		layer.alert("操作成功！");
      		var locationUrl = $("#"+formId).find("input[name='location']").val();
			if(!strUtil.isNull(locationUrl)){
				var pageUrl = ctx+locationUrl; 
				mytools.pjax(pageUrl);//操作完成跳转其他页面
				//location.href=pageUrl;
			}else{
				//本页面
			}
      		setToken();
	  	}
	});
 }
//提交表单数据
function submitForm(formId,tabId){
	var result;
	var index_load = layer.load(1, {shade: false});
	$.ajax({
	    type:'post',
	    url:common_submit_url,
	    data:$("#"+formId).serialize(),
	    async:false,
	    dataType:'json',
	    success: function(myData){ 
	     console.log(myData);
	        if(myData.flag=="T"){
	        	result = myData;
	    	}else{
	    		setToken();
	     		layer.alert(myData.msg);
	    	}
	        layer.close(index_load);
	  	},
	     error: function (e) {
		     layer.close(index_load);
     	} 
	});	
	return result;
}
//刷新
function refresh(){
	getTableData();
}
//设置token
function setToken(){
	$.ajax({
	    type:'get',
	    url:'${r'${ctx}'}/sendPage/token?token=true',
	    dataType:'text',
	    success: function(myData){
	    	console.log(myData);
			$('.formToken').val(myData);
	  	}
	});	
}

/**** Part 三、以下是自定义新增方法  ****/
//1.定义校验规则-定义
function setValidate(id){
	$(id).each(function(){
		$(this).bootstrapValidator({
		    feedbackIcons: {
		        valid: 'glyphicon glyphicon-ok',
		        invalid: 'glyphicon glyphicon-remove',
		        validating: 'glyphicon glyphicon-refresh'
		    },/** 以下需要结合实际业务清空补充个表单元素的验证规则 **/
		 	fields:{
		 		/** 可自定义新增验证规则
		    	"uctUser.userEname":{
		    		 validators: {
		    			 regexp: {
		    				 regexp: /^[0-9]+$/,
		                      message: '只能是数字'
		                  }
		              }
		    	}
		    	**/
			}
		});
	});
}


</script>
</section>
<!-- Main end -->   
</div>
<!-- foot start -->
<%@include file="/common/${projectName}/foot.jsp" %>
<!-- foot start -->
</div>
<!-- /.content-wrapper -->
</body>
</html>
